package com.lzx.reggie.controller

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.lzx.reggie.common.R
import com.lzx.reggie.dto.SetmealDto
import com.lzx.reggie.entity.Setmeal
import com.lzx.reggie.service.CategoryService
import com.lzx.reggie.service.SetmealDishService
import com.lzx.reggie.service.SetmealService
import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/setmeal")

class SetmealController {

    companion object {
        private val log = LoggerFactory.getLogger(SetmealController::class.java)
    }

    @Autowired
    private lateinit var setmealService: SetmealService

    @Autowired
    private lateinit var categoryService: CategoryService

    @Autowired
    private lateinit var setmealDishService: SetmealDishService

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    fun save(@RequestBody setmealDto: SetmealDto): R<String> {
        log.info("套餐信息：{}", setmealDto)
        setmealService.saveWithDish(setmealDto)
        return R.success("新增套餐成功")
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    fun page(page: Long, pageSize: Long, name: String?): R<Page<*>> {
        //分页构造器对象
        val pageInfo = Page<Setmeal>(page, pageSize)
        val dtoPage = Page<SetmealDto>()

        setmealService.page(pageInfo,
            KtQueryWrapper(Setmeal::class.java).apply {
                // 添加查询条件，根据name进行like模糊查询
                like(name != null, Setmeal::name, name)
                // 添加排序条件，根据更新时间降序排列
                orderByDesc(Setmeal::updateTime)
            })

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage, "records")
        val records = pageInfo.records
        val list = records.map { item: Setmeal ->
            val setmealDto = SetmealDto()
            //对象拷贝
            BeanUtils.copyProperties(item, setmealDto)

            // 根据分类id查询分类对象 分类名称
            setmealDto.categoryName = categoryService.getById(
                // 分类id
                item.categoryId
            )?.name

            setmealDto
        }
        dtoPage.records = list
        return R.success(dtoPage)
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    fun delete(@RequestParam ids: List<Long>): R<String> {
        log.info("ids:{}", ids)
        setmealService.removeWithDish(ids)
        return R.success("套餐数据删除成功")
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    fun list(setmeal: Setmeal): R<List<Setmeal>> {
        return R.success(setmealService.list(KtQueryWrapper(Setmeal::class.java).apply {
            eq(setmeal.categoryId != null, Setmeal::categoryId, setmeal.categoryId)
            eq(setmeal.status != null, Setmeal::status, setmeal.status)
            orderByDesc(Setmeal::updateTime)
        }))
    }
}
